$(function(){
    //姓名框
    $("#username").focus(function () {
        $("#username").css("border","1px #e3e3e3 solid");
        $("#tip_username").text("5~16个字符，可使用字母、数字、下划线，需要以字母开头");
        $("#tip_username").css("color","#CCCCCC");
    });
    $("#username").blur(function(){
        var patt = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;
        if (this.value.length == 0){
            $("#username").css("border","1px #e3e3e3 solid");
            $("#username").attr("flag","0");
            $("#tip_username").text("");
        }
        else if(this.value.length < 5 || this.value.length > 16){
            $("#username").css("border","1px #ff0000 solid");
            $("#username").attr("flag","0");
            $("#tip_username").text("长度要在5~16个字符之间");
            $("#tip_username").css("color","#ff0000");
        }
        else if (patt.test(this.value)){
            $.get("/repetitive", "username=" + this.value ,function (data) {
                if (data){
                    $("#username").attr("flag","1");
                    $("#username").css("border","1px #e3e3e3 solid");
                    $("#tip_username").text("用户名可用");
                    $("#tip_username").css("color","#008000");
                } else{
                    $("#username").attr("flag","0");
                    $("#username").css("border","1px #e3e3e3 solid");
                    $("#tip_username").text("用户名已存在");
                    $("#tip_username").css("color","#ff0000");
                }
            })
        }
        else{
            $("#username").attr("flag","0");
            $("#username").css("border","1px #ff0000 solid");
            $("#tip_username").text("使用字母、数字、下划线，需要以字母开头");
            $("#tip_username").css("color","#ff0000");
        }
    });

    $("#password").focus(function () {
        $("#password").css("border","1px #e3e3e3 solid");
        $("#tip_password").text("8~16个字符，可使用字母、数字、下划线，区分大小写");
        $("#tip_password").css("color","#CCCCCC");
    });
    $("#password").blur(function(){
        var patt = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,16}$/;
        if (this.value.length == 0){
            $("#password").attr("flag","0");
            $("#password").css("border","1px #e3e3e3 solid");
            $("#tip_password").text("");
        }
        else if (this.value.length < 8 || this.value.length > 16){
            $("#password").attr("flag","0");
            $("#password").css("border","1px #ff0000 solid");
            $("#tip_password").text("长度要在8~16个字符之间");
            $("#tip_password").css("color","#ff0000");
        }
        else if (patt.test(this.value)){
            $("#password").attr("flag","1");
            $("#password").css("border","1px #e3e3e3 solid");
            $("#tip_password").text("密码符合要求");
            $("#tip_password").css("color","#008000");
        }
        else {
            $("#password").attr("flag","0");
            $("#password").css("border","1px #ff0000 solid");
            $("#tip_password").text("必须是字母+数字的组合");
            $("#tip_password").css("color","#ff0000");
        }
    });

    $("#email").focus(function () {
        $("#email").css("border","1px #e3e3e3 solid");
        $("#tip_email").text("填写邮箱");
        $("#tip_email").css("color","#CCCCCC");
    });
    $("#email").blur(function () {
        var patt = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if (patt.test(this.value)){
            $("#email").attr("flag","1");
            $("#email").css("border","1px #e3e3e3 solid");
            $("#tip_email").text("邮箱格式正确");
            $("#tip_email").css("color","#008000");
        }
        else if(this.value.length == 0){
            $("#email").attr("flag","0");
            $("#email").css("border","1px #e3e3e3 solid");
            $("#tip_email").text("");
            $("#tip_email").css("color","#CCCCCC");
        }
        else {
            $("#email").attr("flag","0");
            $("#email").css("border","1px #ff0000 solid");
            $("#tip_email").text("邮箱格式不正确");
            $("#tip_email").css("color","#ff0000");
        }
    });

    $("#agree").click(function () {
        if ($("#agree").is(":checked")){
            $("#tip_agree").text("");
        }
    })

    $("#register_btn").click(function () {
        var flag = true;
        if (!$("#agree").is(":checked")){
            $("#tip_agree").text("请同意协议");
            $("#tip_agree").css("color","#ff0000");
            flag = false;
        }
        $(".input_txt").each(function (index, domEle) {
            if($(this).attr("flag") == "0"){
                flag = false;
                return false;
            }
        });
        if (!flag){
            alert("注册信息存在问题或没有填");
        } else {
            var username = $("#username").val();
            var password = $("#password").val();
            console.log("run");
            var data = { username:username , password:password};
            console.log(data);
            $.post("/register", data , function (result) {
                console.log(result);
                if (result != null){
                    window.location.href = "/registerSuccess";
                } else {
                    flag = false;
                    alert("注册出现错误");
                }
            }, "json");
        }
        return false;
    });
});